<?php
namespace app\beautiful\helper\payment;
use app\common\helper\payment\PaymentBaseHelper;
use app\beautiful\model\order\OnceCardOrderModel;
use app\beautiful\logic\order\OrderLogic;
use think\facade\Db;
//每个子类只需要专注于自己的 respond 逻辑
class PaymentOncecardHelper extends PaymentBaseHelper{
    protected  $moudel = 'beautiful';
    protected  $orderKey = 'so'; //给一个KEY增大吞吐量
    protected  $respondAction = 'oncecardRespond';
    
    public function respondCallback($orderId,$payInfo,$fail){
        $order = OnceCardOrderModel::find($orderId);
        if(null === $order) return;
        if($order->is_paid == 0 && $order->is_delete== 0){ 
            $order_logic = OrderLogic::init($this->shopId);
            Db::startTrans();
            try{
                $order_logic['once_card']->wxPay([
                    'member_id' => $order->member_id,
                    'once_card_order_id' => $orderId,
                    'payinfo'   => $payInfo
                ]);
                Db::commit();
                return true;
            } catch (\Exception $e){ //退款并取消订单
                Db::rollback();
                $this->refundInTheSameWay($payInfo);
                $order_logic['once_card']->cancelOrder([
                    'member_id' => $order->member_id,
                    'once_card_order_id' => $orderId,
                ]);
                $fail('支付回调处理失败');
            }
            return true;
        }
        
    }
    
}